#include  <fftw3.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<math.h>

int main( int argc, char** argv )
{
	/* Initialize */
	int N = 16;
	fftw_complex *out;
	fftw_plan p;
	double *in;
	int i, j, k;
	double PI = 4.0 * atan(1.0);
	double *inverseout;

	/* Setup */
	in = (double*)malloc(sizeof(double) * N );
	out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * (N/2 + 1) );
	inverseout = (double*)malloc(sizeof(double) * N );

	for( i = 0; i < N; i++ )
	{
		in[i] = sin( 2.0 * PI * (double)(i) / (double)(N) + PI / N );
	}

	/* Compute */	
	p = fftw_plan_dft_r2c_1d( N, in, out, FFTW_ESTIMATE);
	fftw_execute(p);
	fftw_destroy_plan(p);
	for ( k = 0; k < N/2+1 ; k++ )
	{
		printf("(%f, %f)", out[k][0], out[k][1] );
	}

	p = fftw_plan_dft_c2r_1d( N, out, inverseout, FFTW_ESTIMATE);
	fftw_execute(p);
	fftw_destroy_plan(p);
	
	printf("Error=\n");
	for ( i = 0; i < N ; i++ )
	{
		printf("%5.2f, ", fabs( in[i] - inverseout[i] / N ) );
	}

	/* Free space */
	fftw_free(out);
	free(inverseout);
	free(in);
	return EXIT_SUCCESS;
}

